<script lang="ts">
import { defineComponent, ref, type PropType } from "vue";

export default defineComponent({
  props: {
    modelValue: { type: String, required: true },
    status: {
      type: String as PropType<"init" | "search" | "finish">,
      default: "init",
    },
  },
  emits: {
    "update:modelValue": (value: string) => typeof value === "string",
    search: (value: string) => typeof value === "string",
  },
  setup(props, ctx) {
    const updateModelValue = (e: MiniApp.Event<{ value: string }>) => {
      ctx.emit("update:modelValue", e.detail.value);
    };

    const isFocus = ref(false);

    const searchClick = (type: "search" | "clear") => {
      ctx.emit("search", type);
    };

    return { updateModelValue, isFocus, searchClick };
  },
});
</script>

<template>
  <view class="search-bar">
    <view class="search-bar-wrapper">
      <icon type="search" size="32rpx" color="#913e3e" />
      <input
        class="search-bar-input"
        :value="modelValue"
        confirm-type="search"
        placeholder="请输入想要查找的词汇术语"
        @focus="isFocus = true"
        @blur="isFocus = false"
        @input="updateModelValue"
        @confirm="searchClick('search')"
      />
      <icon
        v-show="modelValue.length !== 0"
        type="clear"
        size="32rpx"
        color="#913e3e"
        @click="searchClick('clear')"
      />
    </view>
    <view
      class="search-bar-btn"
      v-if="status === 'finish' || isFocus"
      @click="searchClick('search')"
    >
      搜索
    </view>
  </view>
</template>

<style lang="scss">
.search-bar {
  position: relative;
  display: flex;
  align-items: center;
  padding: 0 40rpx;

  .search-bar-wrapper {
    display: flex;
    align-items: center;
    flex-grow: 1;
    min-width: 0;
    height: 76rpx;
    padding-left: 30rpx;
    padding-right: 20rpx;
    border-radius: 42rpx;
    border: 2rpx solid #b95c47;
    background-color: rgba(255, 255, 255, 0.9);
  }

  .search-bar-input {
    flex-grow: 1;
    min-width: 0;
    margin-left: 10rpx;
    margin-right: 20rpx;
    font-size: 28rpx;
    color: #913e3e;
  }

  .search-bar-btn {
    flex-shrink: 0;
    padding: 0 34rpx;
    margin-left: 20rpx;
    font-size: 28rpx;
    color: #913e3e;
    line-height: 76rpx;
    border-radius: 42rpx;
    border: 2rpx solid #b95c47;
    background: rgba(255, 255, 255, 0.9);
  }
}
</style>